package com.hlkj.warboot.module_sys.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hlkj.warboot.configurer.util.LoggerUtil;
import com.hlkj.warboot.configurer.util.ResponseUtil;
import com.hlkj.warboot.configurer.util.StrUtil;
import com.hlkj.warboot.configurer.util.base.BaseExceptionUtil;
import com.hlkj.warboot.module_sys.entity.PermissionEntity;
import com.hlkj.warboot.module_sys.service.PermissionService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 系统权限（对应页面中的菜单和按钮） 前端控制器
 * </p>
 *
 * @author lixing_java@163.com
 * @since 2020-06-11
 */
@Controller
@RequestMapping("/sys/permission")
public class PermissionControler {
    @Resource
    private PermissionService permissionService;

    /*
     * @Description TODO 列表
     * @Author 李星
     * @Date 2019/2/16 13:08
     * @Param []
     * @return org.springframework.web.servlet.ModelAndView
     */
    @GetMapping("toList")
    public ModelAndView toPage() throws Exception {
        return new ModelAndView("sys/permission/permissionList");
    }

    @PostMapping("list")
    @ResponseBody
    public Object page(PermissionEntity entity) throws Exception {
        LoggerUtil.info(MessageFormat.format("【权限列表】条件：{0}", JSON.toJSONString(entity)));

        QueryWrapper<PermissionEntity> permissionEntityQueryWrapper = new QueryWrapper<PermissionEntity>();
        permissionEntityQueryWrapper.orderByAsc("sort");
        List<PermissionEntity> permissionEntityList = permissionService.list(permissionEntityQueryWrapper);

        LoggerUtil.info(MessageFormat.format("【权限列表】结果：{0}", permissionEntityList.size()));
        return ResponseUtil.success(permissionEntityList);
    }

    /*
     * @Description TODO 跳转到【添加|编辑】菜单页面
     * @Author 李星
     * @Date 2019/9/10 13:57
     * @Param [category add表示添加|update表示更新, id 添加时，表示需要被添加的节点的上级节点编号，编辑时，表示需要被编辑的节点编号]
     * @return org.springframework.web.servlet.ModelAndView
     */
    @GetMapping("toModify/{category}/{id}")
    public ModelAndView toModify(@PathVariable String category, @PathVariable String id) throws Exception {
        ModelAndView modelAndView = new ModelAndView();

        if ("insert".equals(category)) {
            modelAndView.setViewName("sys/permission/permissionAdd");
        } else if ("update".equals(category)) {
            modelAndView.setViewName("sys/permission/permissionUpdate");
        } else {
            throw new BaseExceptionUtil("非法请求");
        }

        PermissionEntity permissionEntity = new PermissionEntity();
        if ("0".equals(id)) {
            permissionEntity.setId("0");
            permissionEntity.setGrade(0);
            permissionEntity.setPermissionName("超级菜单"); // 系统默认超级菜单
        } else {
            permissionEntity = permissionService.getById(id); // 查询权限信息
        }
        modelAndView.addObject("entity", permissionEntity);
        return modelAndView;
    }

    /*
     * @Description TODO 保存菜单编辑
     * @Author 李星
     * @Date 2019/9/10 16:12
     * @Param [entity 菜单信息，opCategory 操作类型]
     * @return java.lang.Object
     */
    @PostMapping("saveModify/{opCategory}")
    @ResponseBody
    public Object saveModify(PermissionEntity entity, @PathVariable String opCategory) throws Exception {
        boolean flag;
        if ("insert".equals(opCategory)) {
            flag = permissionService.save(entity);
        } else if ("update".equals(opCategory)) {
            flag = permissionService.updateById(entity);
        } else {
            throw new BaseExceptionUtil("保存编辑菜单权限页面时获取参数失败");
        }
        if (flag) {
            return ResponseUtil.success();
        } else {
            return ResponseUtil.fail("操作失败");
        }
    }

    /*
     * @Description TODO 删除菜单
     * @Author 李星
     * @Date 2019/9/11 9:14
     * @Param [entity]
     * @return java.lang.Object
     */
    @PostMapping("delete")
    @ResponseBody
    public Object delete(PermissionEntity entity) throws Exception {
        LoggerUtil.info(MessageFormat.format("【菜单删除】条件：{0}", entity.getId()));

        boolean flag;
        if (StrUtil.isEffective(entity.getId())) {
            if (entity.getId().contains(",")) {
                flag = permissionService.removeByIds(Arrays.asList(entity.getId().split(",")));
            } else {
                flag = permissionService.removeById(entity.getId());
            }
            LoggerUtil.info(MessageFormat.format("【菜单删除】结果：{0}", flag));
            return ResponseUtil.success(flag);
        } else {
            return ResponseUtil.fail("主键缺失");
        }
    }
}

